﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace LicenseAutoDetection
{
    public class Registeration
    {
        private const String filename = @"regist.dat";

        public void CreateFile(String pcode) // 인증서 생성
        {
            Byte[] data = Encoding.Default.GetBytes(CMacAddressManager.Address + pcode);

            for (int i = 0; i < data.Length; i++)
            {
                data[i] *= 2;
            }

            try
            {
                if (!System.IO.File.Exists(filename))
                {
                    File.Create(filename).Dispose();
                }

                File.WriteAllBytes(filename, data);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }
        }

        public String ReadPCode() //인증서 파일에서 Pcode 받아오는 함수
        {
            String pcode = "";

            try
            {
                byte[] data = File.ReadAllBytes(filename);

                for (int i = 0; i < data.Length; i++)
                {
                    data[i] /= 2;
                }

                pcode = Encoding.Default.GetString(data).Substring(CMacAddressManager.Address.Length); 
            }
            catch
            {
                // do nothing    
            }

            return pcode;
        }
    }
}
